import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date: 2024-01-30
 * Time:9:35
 */
public class MyArrayList implements IList{
    public int[] elem;
    //有效元素个数
    public int size;
    //数组默认大小
    private static final int DEFAULT_SIZE = 10;

    public MyArrayList() {
        this.elem = new int[DEFAULT_SIZE];
    }


    @Override
    public void display() {
        for (int i = 0; i < size; i++) {
            System.out.print(elem[i] + " ");
        }
        System.out.println();
    }

    private boolean isFull() {
        return this.size == elem.length;
    }

    private void checkCapacity() {
        if(isFull()) {
            elem = Arrays.copyOf(elem,elem.length*2);
        }
    }


    @Override
    public void add(int data) throws ListIsFullException{
        //判断顺序表是否满了，如果满了，就要扩容
        if(isFull()) {
            elem = Arrays.copyOf(elem,elem.length*2);
        }
        elem[size++] = data;
    }

    //在指定下标插入元素时判断下标是否合法
    private boolean checkPosInAdd(int pos) {
        if(pos < 0 || pos > size)
            return false;
        return true;
    }

    @Override
    public void add(int pos, int data) throws IllegalPosException {
        if(!checkPosInAdd(pos)) {
            throw new IllegalPosException("非法下标！");
        }
        if(isFull()) {
            elem = Arrays.copyOf(elem,elem.length*2);
        }
        //从pos下标开始的元素往后挪
        for(int i = size -1;i>=pos;i--) {
            //elem[i+1] = elem[i];
            set(i+1,elem[i]);
        }
        set(pos,data);
        ++size;
    }

    private boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean contains(int toFind) {
        if(isEmpty()) {
            return false;
        }
        for(int i = 0;i < size;i++) {
            if(elem[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        if(isEmpty()) {
            return -1;
        }
        for(int i = 0;i < size;i++) {
            if(elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    public boolean checkPosInGetAndSet(int pos) {
        if(pos < 0 || pos >= size)
            return false;
        return true;
    }

    @Override
    public int get(int pos) {
        if(isEmpty()) {
            throw new MyArrayListIsEmpty("顺序表为空，无法查找！");
        }
        if(!checkPosInGetAndSet(pos)) {
            throw new IllegalPosException("非法下标！");
        }
        return elem[pos];
    }

    @Override
    public void set(int pos, int value) {
        if(!checkPosInGetAndSet(pos)) {
            throw new IllegalPosException("非法下标！");
        }
        elem[pos] = value;
    }

    @Override
    public void remove(int key) {
        //找到元素
        int pos = indexOf(key);
        //它后面的元素往前覆盖
        for(int i = pos;i < size-1;i++) {
            elem[i] = elem[i+1];
        }
        --size;
    }

    public void removeAll(int key) {
        //遍历，得到值为key的元素的个数
        int count = 0;
        for (int i = 0; i < size; i++) {
            if(elem[i] == key)
                ++count;
        }
        while(count-- > 0) {
            remove(key);
        }
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public void clear() {
        this.size = 0;
    }

}
